package com.lingshi.yingzaitownmap.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingshi.yingzaitownmap.entity.domain.Route;
import com.lingshi.yingzaitownmap.entity.dto.PageDTO;
import com.lingshi.yingzaitownmap.entity.result.Result;
import com.lingshi.yingzaitownmap.service.RouteService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;


/**
 * 路线相关接口
 */
@RestController
@RequestMapping("/api/v1/routes")
@Slf4j
@Validated
@RequiredArgsConstructor
public class RouteController {

    private final RouteService routeService;

    /**
     * 分页查询路线信息
     */
    @GetMapping
    public Result getRoutes(@RequestBody PageDTO pageDTO){
        // 日志
        log.info("分页查询路线信息,{}",pageDTO);
        // 查询
        Page<Route> page = routeService.query()
                .select("id","name")
                .orderByAsc("created_at")
                .page(new Page<>(pageDTO.getPage(), pageDTO.getPageSize()));
        // 封装返回信息
        HashMap<String, Object> result = new HashMap<>();
        result.put("list", page.getRecords());
        result.put("total", page.getRecords().size());
        // 返回
        return Result.success(result);
    }

    /**
     * 根据ID获取路线详细信息
     */
    @GetMapping("/{id}")
    public Result getRouteById(@PathVariable String id) {
        // 日志
        log.info("根据id获取路线详细信息:{}", id);
        // id非空判断
        if(StrUtil.isBlank(id)){
            throw new RuntimeException("id不能为空");
        }
        // 查询
        return routeService.getRouteDetailsById(id);
    }

}
